package com.zte.springboot_smbms.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zte.springboot_smbms.pojo.SmbmsRole;
import com.zte.springboot_smbms.service.RolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/roleController")
public class RoleController {
    @Autowired
    private RolesService rolesService;

    @RequestMapping("/findAllRole")
    public String findAllRole(Model model){
        model.addAttribute("roleList",rolesService.findAllRole());
        return "rolelist";
    }

    @RequestMapping("/findRoles")
    public @ResponseBody
    String findRoles(String method){
        if ("getrolelist".equals(method)) {
            ObjectMapper objectMapper = new ObjectMapper();
            String resultString = null;
            try {
                List<SmbmsRole> allRole = rolesService.findAllRole();
                resultString = objectMapper.writeValueAsString(allRole);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
            return resultString;
        }
        return null;
    }
}
